﻿// -----------------------------
// Copyright © Kodi Studios 2012
// -----------------------------

using System;
using System.IO;
using System.Net;
using System.Xml;

namespace Kodi.Picturethrill
{
    public class PublishedUpdate
    {
        #region Constants

        private const string HomeSite = @"http://picturethrill.com";
        private const string LatestVersionPath = HomeSite + @"/Update/PicturethrillUpdate.xml";
        
        #endregion

        #region Methods
        /// <summary>
        /// Since it's a webcall, initialize object explicitly.
        /// </summary>
        public void Init()
        {
            // Check web for updates
            WebClient webClient = new WebClient();

            // Check version
            string latestVersionPathLocal = Path.Combine(Settings.TempPath, Path.GetFileName(LatestVersionPath));
            webClient.DownloadFile(LatestVersionPath, latestVersionPathLocal);
            XmlDocument document = new XmlDocument();
            document.Load(latestVersionPathLocal);

            XmlNode versionNode = document.SelectSingleNode("Application/Latest/Version");
            string versionString = versionNode.InnerText;
            this.Version = Version.Parse(versionString);

            XmlNode urlNode = document.SelectSingleNode("Application/Latest/Url");
            this.Url = urlNode.InnerText;
        }
        #endregion

        #region Public Properties

        public Version Version
        {
            get;
            set;
        }

        public string Url
        {
            get;
            set;
        }

        #endregion
    }
}
